package com.example.log.alarm.web;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;
import java.util.concurrent.*;

/**
 * @program: spring-agent
 * @description:
 * @author: zt22328 赵儒定
 * @create: 2023-05-10 17:24
 **/
@Slf4j
@RestController
public class TestController {

    ExecutorService executorService;

    @RequestMapping("/log/error")
    public Object error() {
        log.error("error", new RuntimeException("222"));
        return "ok";
    }

    @RequestMapping("/log/warn")
    public Object warn() {
        log.warn("warn", new RuntimePermission("111"));
        return "ok";
    }

    @RequestMapping("/createThread")
    public Object createThread() {
        executorService = new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS, new ArrayBlockingQueue<>(1));
        return 1;
    }

    @RequestMapping("/execute")
    public Object execute() {
        int i = new Random().nextInt(10);
        executorService.execute(()->{
            System.out.println(Thread.currentThread().getName());
            if (i%2 == 0){
                throw new RuntimeException("123456");
            }
        });
        executorService.execute(()->{
            System.out.println(Thread.currentThread().getName());
            if (i%2 == 0){
                throw new RuntimeException("123456");
            }
        });
        executorService.execute(()->{
            System.out.println(Thread.currentThread().getName());
            if (i%2 == 0){
                throw new RuntimeException("123456");
            }
        });
        executorService.execute(()->{
            System.out.println(Thread.currentThread().getName());
            if (i%2 == 0){
                throw new RuntimeException("123456");
            }
        });
        executorService.execute(()->{
            System.out.println(Thread.currentThread().getName());
            if (i%2 == 0){
                throw new RuntimeException("123456");
            }
        });
        return i;
    }

}
